package com.ybkj.daijia.server.controller;


import com.ybkj.daijia.SmsTempletContent;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.io.FileNotFoundException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.WebUtils;

@Controller
@RequestMapping(value = "smstemplet")
public class SmsTempletController {

    @Autowired
    private SmsTempletService smsService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @RequestMapping(value = "templet", method = RequestMethod.GET)
    public String templet(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("smstemplet")) {

            model.addAttribute("smslist", smsService.listAll());

            return "smstemplet/templet";
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "updateTemplet", method = RequestMethod.POST)
    public String updateTemplet(Model model, SmsTempletContent smsTempletContent, Long companyId,
        String signName, HttpServletRequest req) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("smstemplet")) {

            Company company = companyService.findOne(companyId);
            company.setSignName(signName);
            String basePath = getBasePath(req);
            companyService.update(company, basePath);

            SmsTemplet sms_driverCreate = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.driverCreate);
            if (null == sms_driverCreate) {
                sms_driverCreate = new SmsTemplet();
                sms_driverCreate.setCompanyId(companyId);
                sms_driverCreate.setContent(smsTempletContent.getDriverCreate());
                sms_driverCreate.setSmsType(SmsTemplet.SmsType.driverCreate);
                smsService.insertSmsTemplate(sms_driverCreate);
            } else {
                if (!sms_driverCreate.getContent().equals(smsTempletContent.getDriverCreate())) {
                    sms_driverCreate.setContent(smsTempletContent.getDriverCreate());
                    smsService.updateByPrimaryKey(sms_driverCreate);
                }
            }

            SmsTemplet sms_driverChangePassword = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.driverChangePassword);
            if (null == sms_driverChangePassword) {
                sms_driverChangePassword = new SmsTemplet();
                sms_driverChangePassword.setCompanyId(companyId);
                sms_driverChangePassword.setContent(smsTempletContent.getDriverChangePassword());
                sms_driverChangePassword.setSmsType(SmsTemplet.SmsType.driverChangePassword);
                smsService.insertSmsTemplate(sms_driverChangePassword);
            } else {
                if (!sms_driverChangePassword.getContent()
                    .equals(smsTempletContent.getDriverChangePassword())) {
                    sms_driverChangePassword
                        .setContent(smsTempletContent.getDriverChangePassword());
                    smsService.updateByPrimaryKey(sms_driverChangePassword);
                }
            }

            SmsTemplet sms_driverMoney = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.driverMoney);
            if (null == sms_driverMoney) {
                sms_driverMoney = new SmsTemplet();
                sms_driverMoney.setCompanyId(companyId);
                sms_driverMoney.setContent(smsTempletContent.getDriverMoney());
                sms_driverMoney.setSmsType(SmsTemplet.SmsType.driverMoney);
                smsService.insertSmsTemplate(sms_driverMoney);
            } else {
                if (!sms_driverMoney.getContent().equals(smsTempletContent.getDriverMoney())) {
                    sms_driverMoney.setContent(smsTempletContent.getDriverMoney());
                    smsService.updateByPrimaryKey(sms_driverMoney);
                }
            }

            SmsTemplet sms_memberCode = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.memberCode);
            if (null == sms_memberCode) {
                sms_memberCode = new SmsTemplet();
                sms_memberCode.setCompanyId(companyId);
                sms_memberCode.setContent(smsTempletContent.getMemberCode());
                sms_memberCode.setSmsType(SmsTemplet.SmsType.memberCode);
                smsService.insertSmsTemplate(sms_memberCode);
            } else {
                if (!sms_memberCode.getContent().equals(smsTempletContent.getMemberCode())) {
                    sms_memberCode.setContent(smsTempletContent.getMemberCode());
                    smsService.updateByPrimaryKey(sms_memberCode);
                }
            }

            SmsTemplet sms_driverCode = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.driverCode);
            if (null == sms_driverCode) {
                sms_driverCode = new SmsTemplet();
                sms_driverCode.setCompanyId(companyId);
                sms_driverCode.setContent(smsTempletContent.getDriverCode());
                sms_driverCode.setSmsType(SmsTemplet.SmsType.memberCode);
                smsService.insertSmsTemplate(sms_driverCode);
            } else {
                if (!sms_driverCode.getContent().equals(smsTempletContent.getDriverCode())) {
                    sms_driverCode.setContent(smsTempletContent.getDriverCode());
                    smsService.updateByPrimaryKey(sms_driverCode);
                }
            }

            SmsTemplet sms_newOrder = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.newOrder);
            if (null == sms_newOrder) {
                sms_newOrder = new SmsTemplet();
                sms_newOrder.setCompanyId(companyId);
                sms_newOrder.setContent(smsTempletContent.getNewOrder());
                sms_newOrder.setSmsType(SmsTemplet.SmsType.newOrder);
                smsService.insertSmsTemplate(sms_newOrder);
            } else {
                if (!sms_newOrder.getContent().equals(smsTempletContent.getNewOrder())) {
                    sms_newOrder.setContent(smsTempletContent.getNewOrder());
                    smsService.updateByPrimaryKey(sms_newOrder);
                }
            }

            SmsTemplet sms_cancelOrder = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.cancelOrder);
            if (null == sms_cancelOrder) {
                sms_cancelOrder = new SmsTemplet();
                sms_cancelOrder.setCompanyId(companyId);
                sms_cancelOrder.setContent(smsTempletContent.getCancelOrder());
                sms_cancelOrder.setSmsType(SmsTemplet.SmsType.cancelOrder);
                smsService.insertSmsTemplate(sms_cancelOrder);
            } else {
                if (!sms_cancelOrder.getContent().equals(smsTempletContent.getCancelOrder())) {
                    sms_cancelOrder.setContent(smsTempletContent.getCancelOrder());
                    smsService.updateByPrimaryKey(sms_cancelOrder);
                }
            }

            SmsTemplet sms_revokeOrder = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.revokeOrder);
            if (null == sms_revokeOrder) {
                sms_revokeOrder = new SmsTemplet();
                sms_revokeOrder.setCompanyId(companyId);
                sms_revokeOrder.setContent(smsTempletContent.getRevokeOrder());
                sms_revokeOrder.setSmsType(SmsTemplet.SmsType.revokeOrder);
                smsService.insertSmsTemplate(sms_revokeOrder);
            } else {
                if (!sms_revokeOrder.getContent().equals(smsTempletContent.getRevokeOrder())) {
                    sms_revokeOrder.setContent(smsTempletContent.getRevokeOrder());
                    smsService.updateByPrimaryKey(sms_revokeOrder);
                }
            }

            SmsTemplet sms_driverAccept = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.driverAccept);
            if (null == sms_driverAccept) {
                sms_driverAccept = new SmsTemplet();
                sms_driverAccept.setCompanyId(companyId);
                sms_driverAccept.setContent(smsTempletContent.getDriverAccept());
                sms_driverAccept.setSmsType(SmsTemplet.SmsType.driverAccept);
                smsService.insertSmsTemplate(sms_driverAccept);
            } else {
                if (!sms_driverAccept.getContent().equals(smsTempletContent.getDriverAccept())) {
                    sms_driverAccept.setContent(smsTempletContent.getDriverAccept());
                    smsService.updateByPrimaryKey(sms_driverAccept);
                }
            }

            SmsTemplet sms_arriveFromPlace = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.arriveFromPlace);
            if (null == sms_arriveFromPlace) {
                sms_arriveFromPlace = new SmsTemplet();
                sms_arriveFromPlace.setCompanyId(companyId);
                sms_arriveFromPlace.setContent(smsTempletContent.getArriveFromPlace());
                sms_arriveFromPlace.setSmsType(SmsTemplet.SmsType.arriveFromPlace);
                smsService.insertSmsTemplate(sms_arriveFromPlace);
            } else {
                if (!sms_arriveFromPlace.getContent()
                    .equals(smsTempletContent.getArriveFromPlace())) {
                    sms_arriveFromPlace.setContent(smsTempletContent.getArriveFromPlace());
                    smsService.updateByPrimaryKey(sms_arriveFromPlace);
                }
            }

            SmsTemplet sms_orderFinished = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.orderFinished);
            if (null == sms_orderFinished) {
                sms_orderFinished = new SmsTemplet();
                sms_orderFinished.setCompanyId(companyId);
                sms_orderFinished.setContent(smsTempletContent.getOrderFinished());
                sms_orderFinished.setSmsType(SmsTemplet.SmsType.orderFinished);
                smsService.insertSmsTemplate(sms_orderFinished);
            } else {
                if (!sms_orderFinished.getContent().equals(smsTempletContent.getOrderFinished())) {
                    sms_orderFinished.setContent(smsTempletContent.getOrderFinished());
                    smsService.updateByPrimaryKey(sms_orderFinished);
                }
            }

            SmsTemplet sms_modifyCash = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.modifyCash);
            if (null == sms_modifyCash) {
                sms_modifyCash = new SmsTemplet();
                sms_modifyCash.setCompanyId(companyId);
                sms_modifyCash.setContent(smsTempletContent.getModifyCash());
                sms_modifyCash.setSmsType(SmsTemplet.SmsType.modifyCash);
                smsService.insertSmsTemplate(sms_modifyCash);
            } else {
                if (!sms_modifyCash.getContent().equals(smsTempletContent.getModifyCash())) {
                    sms_modifyCash.setContent(smsTempletContent.getModifyCash());
                    smsService.updateByPrimaryKey(sms_modifyCash);
                }
            }

            SmsTemplet sms_yupaiOrder = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.yupaiOrder);
            if (null == sms_yupaiOrder) {
                sms_yupaiOrder = new SmsTemplet();
                sms_yupaiOrder.setCompanyId(companyId);
                sms_yupaiOrder.setContent(smsTempletContent.getYupaiOrder());
                sms_yupaiOrder.setSmsType(SmsTemplet.SmsType.yupaiOrder);
                smsService.insertSmsTemplate(sms_yupaiOrder);
            } else {
                if (!sms_yupaiOrder.getContent().equals(smsTempletContent.getYupaiOrder())) {
                    sms_yupaiOrder.setContent(smsTempletContent.getYupaiOrder());
                    smsService.updateByPrimaryKey(sms_yupaiOrder);
                }
            }

            SmsTemplet sms_memberRechargeRecord = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.memberRechargeRecord);
            if (null == sms_memberRechargeRecord) {
                sms_memberRechargeRecord = new SmsTemplet();
                sms_memberRechargeRecord.setCompanyId(companyId);
                sms_memberRechargeRecord.setContent(smsTempletContent.getMemberRechargeRecord());
                sms_memberRechargeRecord.setSmsType(SmsTemplet.SmsType.memberRechargeRecord);
                smsService.insertSmsTemplate(sms_memberRechargeRecord);
            } else {
                if (!sms_memberRechargeRecord.getContent()
                    .equals(smsTempletContent.getMemberRechargeRecord())) {
                    sms_memberRechargeRecord
                        .setContent(smsTempletContent.getMemberRechargeRecord());
                    smsService.updateByPrimaryKey(sms_memberRechargeRecord);
                }
            }

            SmsTemplet sms_memberConsumedRecord = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.memberConsumedRecord);
            if (null == sms_memberConsumedRecord) {
                sms_memberConsumedRecord = new SmsTemplet();
                sms_memberConsumedRecord.setCompanyId(companyId);
                sms_memberConsumedRecord.setContent(smsTempletContent.getMemberConsumedRecord());
                sms_memberConsumedRecord.setSmsType(SmsTemplet.SmsType.memberConsumedRecord);
                smsService.insertSmsTemplate(sms_memberConsumedRecord);
            } else {
                if (!sms_memberConsumedRecord.getContent()
                    .equals(smsTempletContent.getMemberConsumedRecord())) {
                    sms_memberConsumedRecord
                        .setContent(smsTempletContent.getMemberConsumedRecord());
                    smsService.updateByPrimaryKey(sms_memberConsumedRecord);
                }
            }

            SmsTemplet sms_activity = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.activity);
            if (null == sms_activity) {
                sms_activity = new SmsTemplet();
                sms_activity.setCompanyId(companyId);
                sms_activity.setContent(smsTempletContent.getActivity());
                sms_activity.setSmsType(SmsTemplet.SmsType.activity);
                smsService.insertSmsTemplate(sms_activity);
            } else {
                if (!sms_activity.getContent().equals(smsTempletContent.getActivity())) {
                    sms_activity.setContent(smsTempletContent.getActivity());
                    smsService.updateByPrimaryKey(sms_activity);
                }
            }

            SmsTemplet sms_recharge = smsService
                .findByCompanyAndType(companyId, SmsTemplet.SmsType.recharge);
            if (null == sms_recharge) {
                sms_recharge = new SmsTemplet();
                sms_recharge.setCompanyId(companyId);
                sms_recharge.setContent(smsTempletContent.getRecharge());
                sms_recharge.setSmsType(SmsTemplet.SmsType.recharge);
                smsService.insertSmsTemplate(sms_recharge);
            } else {
                if (!sms_recharge.getContent().equals(smsTempletContent.getRecharge())) {
                    sms_recharge.setContent(smsTempletContent.getRecharge());
                    smsService.updateByPrimaryKey(sms_recharge);
                }
            }

            String detail = "【%s】修改了短信模板";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            return "redirect:/company/list";
        } else {
            throw new NoPermissionException();
        }

    }

    private String getBasePath(HttpServletRequest req) {

        try {
            return WebUtils.getRealPath(req.getSession().getServletContext(), "/");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return StringUtils.EMPTY;
    }
}
